package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/courses")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    //1. 课程列表查询
    @GetMapping
    public Result getList(CourseDto courseDto){
        log.info("课程列表请求信息:{}",courseDto);
        PageResult<Course> pageResult=courseService.getList(courseDto);
        return Result.success(pageResult);
    }

    //2. 删除课程
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        log.info("删除课程id:{}",id);
        courseService.deleteById(id);
        return Result.success();
    }

    //3. 添加课程
    @PostMapping
    public Result addCourse(@RequestBody Course course){
        log.info("添加课程数据:{}",course);
        courseService.addCourse(course);
        return Result.success();
    }

    //4. 根据ID查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询-id={}",id);
        Course course= courseService.getById(id);
        return Result.success(course);
    }

    //5. 修改课程
    @PutMapping
    public Result updateById(@RequestBody Course course){
        log.info("修改课程:{}",course);
        courseService.updateById(course);
        return Result.success();
    }

    //6. 查询所有课程
    @GetMapping("/list")
    public Result getListCourse(){
        log.info("查询所有课程");
        List<Course> list=courseService.getListCourse();
        return Result.success(list);
    }

    //7. 根据学科查询课程
    @GetMapping("/subject/{subject}")
    public Result getListBySubject(@PathVariable Integer subject){
        log.info("根据学科查询课程: subject={}",subject);
        List<Course> list=courseService.getListBySubject(subject);
        return Result.success(list);
    }

}
